#include "UILayer.h"


GameUILayer::GameUILayer(void)
{
	MYLOG("%s:GameUILayer",typeid(this).name());
}


GameUILayer::~GameUILayer(void)
{
	MYLOG("%s:~GameUILayer",typeid(this).name());
}
bool GameUILayer::init(){
	MYLOG("%s:init",typeid(this).name());

	CCSize ss = CCDirector::sharedDirector()->getWinSize();
	WIN_W = ss.width;
	WIN_H = ss.height;

	//initRocker();
	initControl();

	CCSpriteBatchNode* lightNode = CCSpriteBatchNode::create("map_land.png");
	addChild(lightNode,0,kLAYER_LIGHTNODE);
	lightNode->setAnchorPoint(ccp(0,0));
	lightNode->setPosition(ccp(0,0));

	CCNotificationCenter::sharedNotificationCenter()->addObserver(
		this,callfuncO_selector(GameUILayer::onEatReward),EVENT_EAT_REWARD,NULL);

	return true;
}

void GameUILayer::onEatReward(CCObject* obj){
	MYLOG("%s:onEatReward",typeid(this).name());
	EventObject* eObj = (EventObject*)obj;
	CCAssert(eObj->data,"event data is null");
	CCPoint p = *(CCPoint*)(eObj->data);
	CCSprite* sp = CCSprite::createWithSpriteFrameName("light.png");
	CCNode* lightNode = getChildByTag(kLAYER_LIGHTNODE);
	lightNode->addChild(sp);
	sp->setPosition(p);
	sp->setColor(ccc3(255*CCRANDOM_0_1(),255*CCRANDOM_0_1(),255*CCRANDOM_0_1()));

	CCPoint endp = ccp(10+sp->getContentSize().width/2,WIN_H-10-sp->getContentSize().height/2);
	bezierAction(sp,p,endp,RANG_FLOAT(0.5f,1.5f));
}
void GameUILayer::bezierAction(CCSprite* sp, CCPoint from, CCPoint to,float dt){
	ccBezierConfig cfg;
	cfg.endPosition = to;
	cfg.controlPoint_1 = ccp(RANG_FLOAT(to.x,from.x),from.y);
	cfg.controlPoint_2 = ccp(to.x,RANG_FLOAT(from.y,to.y));
	CCBezierTo* action  = CCBezierTo::create(dt,cfg);
	CCCallFuncN* callback = CCCallFuncN::create(this,callfuncN_selector(GameUILayer::removeSelf));
	sp->runAction(CCSequence::createWithTwoActions(action,callback));
}
void GameUILayer::removeSelf(CCNode* node){
	node->removeFromParentAndCleanup(true);
}

void GameUILayer::initRocker(){
	MYLOG("%s:initRocker",typeid(this).name());

	CCSprite* bg = CCSprite::create("joystick_bg.png");
	CCSprite* front = CCSprite::create("joystick_front.png");
	m_rocker = HRocker::HRockerWithCenter(ccp(0,0),56,front,bg,true);
	this->addChild(m_rocker,1);
}
void GameUILayer::initControl(){
	UILayer* layer = UILayer::create();
	addChild(layer);
	//left
	leftBtn = UIButton::create();
	leftBtn->setTouchEnabled(true);
	leftBtn->setNormalTexture("ctrl_up.png",UI_TEX_TYPE_PLIST);
	leftBtn->setPressedTexture("ctrl_down.png",UI_TEX_TYPE_PLIST);
	leftBtn->setAnchorPoint(ccp(1,0.5));
	leftBtn->setPosition(ccp(WIN_W/2-20,160));
	layer->addWidget(leftBtn);
	//right
	rightBtn = UIButton::create();
	rightBtn->setTouchEnabled(true);
	rightBtn->setNormalTexture("ctrl_up.png",UI_TEX_TYPE_PLIST);
	rightBtn->setPressedTexture("ctrl_down.png",UI_TEX_TYPE_PLIST);
	rightBtn->setFlipX(true);
	rightBtn->setAnchorPoint(ccp(0,0.5));
	rightBtn->setPosition(ccp(WIN_W/2+20,160));
	layer->addWidget(rightBtn);
	//center
	jumpBtn = UIButton::create();
	jumpBtn->setTouchEnabled(true);
	jumpBtn->setNormalTexture("ctrl_jump.png",UI_TEX_TYPE_PLIST);
	jumpBtn->setAnchorPoint(ccp(0.5,0.5));
	jumpBtn->setPosition(ccp(WIN_W/2,155));
	layer->addWidget(jumpBtn);

	//events
	leftBtn->addTouchEventListener(this,toucheventselector(GameUILayer::onControlTouch));
	rightBtn->addTouchEventListener(this,toucheventselector(GameUILayer::onControlTouch));
	jumpBtn->addTouchEventListener(this,toucheventselector(GameUILayer::onControlTouch));

	//anim
	CCActionInterval* leftMove = CCEaseBackIn::create(CCMoveBy::create(0.85f,ccp(-80,0)));
	CCActionInterval* rightMove = CCEaseBackIn::create(CCMoveBy::create(0.85f,ccp(80,0)));
	leftBtn->runAction(CCSequence::create(CCDelayTime::create(1),leftMove,NULL));
	rightBtn->runAction(CCSequence::create(CCDelayTime::create(1),rightMove,NULL));
}
void GameUILayer::onControlTouch(CCObject* pSender,TouchEventType type){
	switch (type)
	{
	case TOUCH_EVENT_BEGAN:
		if(pSender == leftBtn){
			CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_CONTROL_CHANGE_LEFT);
		}else if(pSender == rightBtn){
			CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_CONTROL_CHANGE_RIGHT);
		}else if(pSender == jumpBtn){
			CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_CONTROL_CHANGE_JUMP);
		}
		break;
	case TOUCH_EVENT_ENDED:
	case TOUCH_EVENT_CANCELED:
		if(pSender == leftBtn){
			CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_CONTROL_CHANGE_STOP);
		}else if(pSender == rightBtn){
			CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_CONTROL_CHANGE_STOP);
		}
		break;
	default:
		break;
	}
}